<?php

namespace Lib;

use Models\RolePermission;
use Models\Setting;
use Models\User;
use \Phalcon\Events\Event;
use Phalcon\Exception;
use \Phalcon\Mvc\Dispatcher;

class Acl extends \Phalcon\Mvc\User\Component
{
    public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher)
    {
        //手动捕获ACL异常，phalcon不能捕获
        try {
            //读取设置
            $setting = array();
            foreach (Setting::find() as $settings) {
                $setting[$settings->skey] = $settings->svalue;
            }
            $this->view->setVar("setting", $setting);
            $this->view->setVar("PageTitle", isset($this->config->acl->{$dispatcher->getControllerName()}->{$dispatcher->getActionName()}) ? $this->config->acl->{$dispatcher->getControllerName()}->{$dispatcher->getActionName()} : '其他');
            //设置acl
            $this->view->setVar("acl", $this->config->acl);

            if ($dispatcher->getControllerName() == 'appwechat') {
                return;
            }
            //登录相关界面不走ACL
            if ($dispatcher->getControllerName() == 'sign') {
                return;
            }
            $user = $this->getUser();
            if ($user) {
                $this->getDI()['Cuser'] = function () use ($user) {
                    return $user;
                };
                //查找当前用户的应用
                $appuser = $user->getAppUser("end>'" . date("Y-m-d H:i:s", strtotime("now")) . "'");
                $this->view->setVar("appuser", $appuser);
                //检查是否拥有该应用
                $has_app = $this->isHasApp($user);
                foreach (RolePermission::findByRoleId(intval($user->role_id)) as $value) {
                    $Cpermission[$value->controller][$value->action] = TRUE;
                }
                //如果访问应用，没购买该应用的话要检查权限，购买了该应用则不检查权限，默认该应用可以访问
                if (!$has_app) {
                    if (!$Cpermission[$dispatcher->getControllerName()][$dispatcher->getActionName()]) {
                        $this->response->redirect("admin/index");
                        return false;
                    }
                }
                $this->view->setVar("Cpermission", $Cpermission);
                $this->view->setVar("cu", $user->toArray());
                $this->view->setVar("controller", $dispatcher->getControllerName());
                $this->view->setVar("action", $dispatcher->getActionName());
            } else {
                $this->response->redirect("admin/sign/login");
                return false;
            }
        } catch (Exception $e) {
            $this->view->disable();
            echo $e->getMessage();
            return false;
        }
    }

    public function isHasApp($user)
    {
        $controller = $this->dispatcher->getControllerName();
        $appuser = $user->getAppUser("end>'" . date("Y-m-d H:i:s", strtotime("now")) . "'");
        foreach ($appuser as $AppUser) {
            if (substr($controller, 3) == $AppUser->App->action || substr($controller, 3) == "main") {
                return true;
            }
        }
        return false;
    }

    public function getUser()
    {
        $user = $this->session->get('user');
        if (!$user) return false;
        $user = User::findFirst($user['uid']);
        if ($user) {
            return $user;
        } else {
            return false;
        }
    }

}